1.5 - C# List and arrays


What are Lists?

Lists are basically containers of variables. They can containe multiple values, as long as they are of the same defined type.


List numbers = new() { 1, 2, 3, 4, 5 }; // declaring a list of type int named "numbers"            
            

// another way to organize the code for readability   
List numbers = new() { 
    1, 2, 3, 4, 5 
};       
            

In the above example, we have basically declared five int variables, which are contained inside the numbers list.
Lists are very useful and the game also uses them a lot.
They can keep the code more clean by not having to declare multiple variables, or group some which have the same usage.

Taking a Sons of the Forest list example, the ItemData list as we can see both in code and dnSpy (but also UnityExplorer) contains all the the informations about each game item (e.g ID, name etc.).


protected override void OnGameStart()
{
    ItemDatabaseManager._instance._itemDataList; // this list contains the data for each game item
}
            

What are Arrays?

Arrays are similar to Lists, with the difference that we can't change their size after creating them.


protected override void OnGameStart()
{
    int[] array = { 1, 2, 3, 4, 5 }; // declaring an array of type int with 5 values
}
            

I won't explain more of them as I nearly never need to use them for mods unlike Lists.